﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using WebShop.Contracts;
using WebShop.DAL.Interfaces;
using WebShop.Infrastructure;
using WebShop.Models;
using WebShop.ViewModels;

namespace WebShop.Controllers
{
    [Authorize(Roles=Constants.ROLE_ADMIN)]
    [HandleErrors]
    public class UsersController : Controller
    {
        [Dependency]
        public IUserRepository _users { get; set; }

        [Dependency]
        public IRoleRepository _roles { get; set; }

        private const int PAGE_SIZE = 20;
        //
        // GET: /User/

        public ViewResult Index(int pageIndex = 1)
        {
            return View(_users.FindList(pageIndex, PAGE_SIZE, null, u=> u.UserName));
        }

        //
        // GET: /User/Details/5

        public ViewResult Details(int id)
        {
            User user = _users.Get(id);
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                _users.Create(user);
                _users.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            UserWithRoles model = new UserWithRoles();
            User user = _users.Get(id);
            IEnumerable<Role> allRoles = _roles.List();
            foreach (Role role in allRoles)
            {
                UserRole ur = new UserRole();
                ur.Name = role.RoleName;
                ur.Participate = user.Roles.SingleOrDefault(r => r.RoleName == role.RoleName) != null;
                model.Roles.Add(ur);
                model.User = user;
            }

            return View(model);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(UserWithRoles model, FormCollection data)
        {
            if (ModelState.IsValid)
            {
                User user = _users.Get(model.User.UserId);
                user.UserName = model.User.UserName;
                user.Email = model.User.Email;
                user.Comments = model.User.Comments;
                user.IsActivated = model.User.IsActivated;
                user.ContactName = model.User.ContactName;
                user.Address = model.User.Address;
                user.City = model.User.City;
                user.Region = model.User.Region;
                user.PostalCode = model.User.PostalCode;
                user.Country = model.User.Country;
                user.Phone = model.User.Phone;
                user.Fax = model.User.Fax;

                IEnumerable<Role> roles = _roles.List();

                user.Roles.Clear();
                foreach (var item in model.Roles.Where(r => r.Participate))
                {
                    Role role = roles.FirstOrDefault(r => r.RoleName == item.Name);
                    if (user.Roles.FirstOrDefault(r => r.RoleName == item.Name) == null)
                    {
                        user.Roles.Add(role);
                    }
                }

                _users.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            User user = _users.Get(id);
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _users.Delete(id);
            _users.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            //db.Dispose();
            base.Dispose(disposing);
        }
    }
}